﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class ShelfService : BaseService, IShelfService
    {
        private readonly IWarehouseRepository _warehouseRepository;
        private readonly IAreaRepository _areaRepository;
        private readonly IRoutewayRepository _routewayRepository;
        private readonly IShelfRepository _shelfRepository;
        private readonly IUnitOfWork _unitOfWork;
        public ShelfService(IMapper mapper, IWarehouseRepository warehouseRepository, IAreaRepository areaRepository, IRoutewayRepository routewayRepository, IShelfRepository shelfRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            _warehouseRepository = warehouseRepository;
            _areaRepository = areaRepository;
            _routewayRepository = routewayRepository;
            _shelfRepository = shelfRepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddShel(Shelf obj, out string message)
        {
            message = string.Empty;
            var sobj = _shelfRepository.QueryIsEntityWhere(x => x.Code == obj.Code);
            if (sobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if(obj.WhId<=0)
            {
                message = "请选择库房！";
                return false;
            }
            //库房
            var wobj = _warehouseRepository.QueryEntityById(obj.WhId);
            if (wobj != null && wobj.Status == 1)
            {
                obj.WhName = wobj.Name;
            }
            else
            {
                message = "库房不存在或已被禁用！";
                return false;
            }
            if (obj.AreaId <= 0)
            {
                message = "请选择库区！";
                return false;
            }
            //库区
            var bobj = _areaRepository.QueryEntityById(obj.AreaId);
            if (bobj != null && bobj.Status == 1)
            {
                obj.AreaName = bobj.Name;
                obj.AreaCode = bobj.Code;
            }
            else
            {
                message = "库区不能再或已被禁用！";
                return false;
            }
            if(obj.RoutewayId<=0)
            {
                message = "请选择巷道！";
                return false;
            }
            _shelfRepository.AddEntity(obj);
            return _unitOfWork.SaveChangesEntity();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelShel(long id)
        {
            var aobj = _shelfRepository.QueryEntityById(id);
            if (aobj != null)
            {
                _shelfRepository.DelEntity(aobj);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        /// <summary>
        /// 根据库区获取货架
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Shelf> GetAreaShelf(long aid)
        {
            var shelflist = _shelfRepository.GetEntityWhereAll(x => x.AreaId == aid);
            return shelflist.ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Shelf? GetShelById(long id)
        {
            return _shelfRepository.QueryEntityById(id);
        }

        /// <summary>
        /// 获取货架列表
        /// </summary>
        /// <param name="hname"></param>
        /// <param name="kid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Shelf> GetShelList(string? hname, long kid, int pageIndex, int pageSize, out int totalCount)
        {
            var shelflist = _shelfRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(hname))
                shelflist = shelflist.Where(x => x.Name.Contains(hname));

            if (kid > 0)
                shelflist = shelflist.Where(x => x.WhId == kid);

            var list = _shelfRepository.GetEntityAlls(shelflist, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        /// <summary>
        /// 添加仓位显示货架
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Menuitem> GetWarAreaShellList()
        {
            var wlist = _warehouseRepository.GetEntityAlls();

            return GetWarAreaShellList(wlist.ToList());
        }
        /// <summary>
        /// 库房
        /// </summary>
        /// <param name="wlist"></param>
        /// <returns></returns>
        public List<Menuitem> GetWarAreaShellList(List<Warehouse> wlist)
        {
            return wlist.Select(x => new Menuitem
            {
                value = x.Id,
                label = x.Name,
                alias = "wh",
                children = GetWarAreaShellList(x.Id)
            }).ToList();
        }
        /// <summary>
        /// 库区
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        public List<Menuitem> GetWarAreaShellList(long wid)
        {
            var alist = _areaRepository.GetEntityWhereAll(x => x.WhId == wid).ToList();
            return alist.Select(x => new Menuitem
            {
                value = x.Id,
                label = x.Name,
                alias = "area",
                children = GetWarAreaShellList(x.Code)
            }).ToList();
        }
        /// <summary>
        /// 货架
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        public List<Menuitem> GetWarAreaShellList(string acode)
        {
            var slist = _shelfRepository.GetEntityWhereAll(x => x.AreaCode == acode).ToList();
            return slist.Select(x => new Menuitem
            {
                value = x.Id,
                label = x.Name,
                alias = "shelf",
                children = null
            }).ToList();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="u"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdShel(Shelf u, out string message)
        {
            message = string.Empty;
            var sobj =_shelfRepository.QueryEntityWhere(x => x.Id != u.Id && x.Code == u.Code);

            if (sobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if (u.WhId <= 0)
            {
                message = "请选择库房！";
                return false;
            }
            //库房
            var wobj = _warehouseRepository.QueryEntityById(u.WhId);
            if (wobj != null && wobj.Status == 1)
            {
                u.WhName = wobj.Name;
            }
            else
            {
                message = "库房不存在或已被禁用！";
                return false;
            }
            if (u.AreaId <= 0)
            {
                message = "请选择库区！";
                return false;
            }
            //库区
            var aobj = _areaRepository.QueryEntityById(u.AreaId);
            if (aobj != null && aobj.Status == 1)
            {
                u.AreaName = aobj.Name;
                u.AreaCode = aobj.Code;
            }
            else
            {
                message = "库区不存在或已被禁用！";
                return false;
            }
            if (u.RoutewayId <= 0)
            {
                message = "请选择巷道！";
                return false;
            }
            _shelfRepository.UpdEntity(u);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
